<?php
/* SVN FILE: $Id: bbcode.php 426 2006-11-15 14:13:48Z phpnut $ */

/**
 * CodeDownload Helper.
 *
 *
 * PHP versions 4 and 5
 *
 * Cake Bakery
 *
 * Copyright (c) 2006,	Cake Software Foundation, Inc.
 * 							1785 E. Sahara Avenue, Suite 490-204
 * 							Las Vegas, Nevada 89104
 *
 * Licensed under the CAKE SOFTWARE FOUNDATION LICENSE(CSFL) version 1.0
 * Redistributions of files must retain the above copyright notice.
 * You may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at:
 * License page: http://www.cakefoundation.org/licenses/csfl/
 * Copyright page: http://www.cakefoundation.org/copyright/
 *
 * @filesource
 * @copyright		Copyright (c) 2006, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
 * @package			bakery
 * @subpackage		bakery.views.helpers
 * @since			Bakery v 1.0.0.0
 * @version			$Revision: 426 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2006-11-15 15:13:48 +0100 (Mi, 15 Nov 2006) $
 * @license			http://www.cakefoundation.org/licenses/csfl/  The CSFL License
 */

/**
 * Short description for file.
 *
 * Long description for file
 *
 * @package      bakery
 * @subpackage   bakery.views.helpers
 * @since        Bakery v 1.0.0.0
 */
if (!class_exists('BbcodeHelper')) {
	require_once(HELPERS.'bbcode.php');
}

class BbcodeDownloadHelper extends BbcodeHelper
{
	/**
	 * Helpers used in this Helper
	 *
	 * @var array
	 */
	var $helpers = array('Html');
	
	/**
	 * Options to be used for processing the Bbcode
	 *
	 * @var array
	 */
	var $options = array('action' => 'format');
	
	/**
	 * Contains the count of the current code block
	 *
	 * @var integer
	 */
	var $codeBlockCount = 0;
	
	/**
	 * Contains the last extracted code block
	 *
	 * @var string
	 */
	var $extractedBlock = null;
	

	/**
	 * This function overwrites BbcodeHelper::format in order to place add download links
	 * to all code blocks found in $string
	 *
	 * @param string $string
	 * @param string $downloadTitle
	 * @param string $downloadUrl
	 * @return string
	 */
	function format($string, $downloadTitle, $downloadUrl) {	
		$this->options['action'] = 'format';
		$this->options['downloadTitle'] = $downloadTitle;
		$this->options['downloadUrl'] = $downloadUrl;
		
		return parent::format($string);
	}
	
	/**
	 * This function can be used to extract the code block at $blockPosition
	 *
	 * @param string $string
	 * @param integer $blockPosition
	 * @return string
	 */
	function extractCodeBlock($string, $blockPosition)	{
		$this->options['action'] = 'extractCodeBlock';
		$this->options['blockPosition'] = $blockPosition;
		
		parent::format($string);
		
		return $this->extractedBlock;
	}
	
	/**
	 * This function overwrites BbcodeHelper::__highlightCode in order to hook into the
	 * code block processing. This is needed to add the code download links and to make
	 * it possible to extract code blocks.
	 *
	 * @param string $result
	 * @return mixed
	 */
	function __highlightCode($result) {
		$this->codeBlockCount++;
		
		switch ($this->options['action']) {
			case 'extractCodeBlock':
				if ($this->codeBlockCount==$this->options['blockPosition']) {					
					$this->extractedBlock = $result;
				}
			break;
			case 'format':
			default:
				$downloadLink = $this->Html->link($this->options['downloadTitle'], sprintf($this->options['downloadUrl'], $this->codeBlockCount));			
				return $downloadLink.parent::__highlightCode($result);
			break;		
		}
	}
		
	/**
	 * This function overwrites BbcodeHelper::_reset in order to reset our custom member
	 * varaibles in this helper
	 *
	 */
	function _reset()	{
		parent::__reset();
		
		$this->codeBlockCount = 0;
		$this->extractedBlock = null;
	}
}

?>